<?php
/**
 * 商品管理模块
 * author:xjy
 * **/
namespace app\index\controller;

use app\common\YujisoController;

class Produce extends YujisoController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getList()
    {
       $list= \app\index\service\ProduceService::getSkuLifeCycleList($this->param);
        exitJson($list);
    }
    
    public function edit()
    {
        $param = $this->request->param();
        if(empty($param)){
             returnJson(0,'参数不能为空');
         }
        if ($this->request->isPost()) {   
           $rules=[];
           $id=$param['id'];
           unset($param['id']);
           foreach($param as $key=>$value){
               $rules[$key]='require';  
           }
          if($param['is_time']==2){
               $param['seasonal_cycle']='';
               unset($rules['seasonal_cycle']);
               unset($rules['seasonal_end_time']);
               unset($rules['seasonal_start_time']);
            }else{
                if($param['seasonal_start_time'] && $param['seasonal_end_time'] && ((int)$param['seasonal_end_time'] > (int)$param['seasonal_start_time'])){
                    $param['seasonal_cycle']=$param['seasonal_start_time']."-".$param['seasonal_end_time'];
                }else{
                   returnJson(0,'开始时间或结束时间不能为空或者开始时间不能大于结束时间'); 
                }
            }
            $this->validateData($rules); 
             unset($param['seasonal_end_time']);
             unset($param['seasonal_start_time']);
             unset($param['s']);
            \app\common\model\ToolModel::updateData(['id' => $id],$param,'sku_life_cycle');
            returnJson(1,'修改成功');
        } else {      
            $user = \app\common\model\ToolModel::findByAttributes('sku_life_cycle', ['id' => $param['id']]);
            returnJson(1, '', ['list' => $user]);
        }
    }
     //启用 禁用
    public function disableOrEnable()
    {
        $param =$this->param;
        $rules = [
            'id' => 'require',
        ];
        $this->validateData($rules);
        \app\common\model\ToolModel::updateData(['id' => $param['id']], ['status' => $param['status']],'sku_life_cycle');
        returnJson(1,'成功');
    }
    public function add()
    {
        $rules=[];
         $param = $this->request->param();
         if(empty($param)){
             returnJson(0,'参数不能为空');
         }
         unset($param['id']);
        foreach($param as $key=>$value){
            $rules[$key]='require';  
        }
       if($param['is_time']==2){
            unset($rules['seasonal_cycle']);
            unset($rules['seasonal_end_time']);
            unset($rules['seasonal_start_time']);
         }else{
             if($param['seasonal_start_time'] && $param['seasonal_end_time'] && ((int)$param['seasonal_end_time'] > (int)$param['seasonal_start_time'])){
                 $param['seasonal_cycle']=$param['seasonal_start_time']."-".$param['seasonal_end_time'];
             }else{
                returnJson(0,'开始时间或结束时间不能为空或者开始时间不能大于结束时间'); 
             }
         } 
        $this->validateData($rules); 
        unset($param['seasonal_end_time']);
        unset($param['seasonal_start_time']);
        unset($param['s']);
        $param['update_time']=date('Y-m-d H:i:s');
        $param['operator']=CN_NAME;
        $one=\app\common\model\ToolModel::findByAttributes('sku_life_cycle',['SKU'=>$param['SKU']]);
        if($one){
            returnJson(0,'添加失败，SKU已经存在'); 
        }
        \app\common\model\ToolModel::insertData($param,'sku_life_cycle');
        returnJson(1,'添加成功'); 
    }
    public function batchOperator()
    {
        $type = $this->request->param('batchType');
        $param=[];
        $param['title']=['SKU','陈列类','大类','中类','小类','细类','销售状态（1-新产品；2-清仓品；在售品；4-下架品）','生命周期（1-补货期；2-新品期；3-计划淘汰期；4-淘汰期','sku等级','季节新产品（1-是，2-否）','季节开始月份','季节结束月份'];//title
        $param['actionAsync']='\app\index\service\ProduceService::uploadHandle';//涓婁紶鏁版嵁鐨勬暟鎹洖璋冨埌service灞傚鐞�
        $param['type']='\app\index\service\ProduceService::uploadHandle';//涓婁紶鏁版嵁鐨勬暟鎹洖璋冨埌service灞傚鐞�
        $param['batchType']=$type;         
        $res=\app\common\service\ExcelLibraryService::read($param);
        exitJson($res);
    }
    public function getSkuName(){
        $list=[];
        $sku = $this->request->param('sku');
        if(is_string($sku)){
            $sku=[$sku];
        }
        $return=\app\common\service\SkuService::getSkuBySku($sku);
        if($return){
            foreach($return as $key=>$value){
              $list[] = $value;
            }
        }
        returnJson(1,'',$list);   
    }
}
